package com.hehao.java_basic.doc_poi;
import com.hehao.java_basic.doc_poi.service.PoiService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author Damon
 * @date 2022/8/1
 */
@RestController
@RequestMapping("/import")
@Tag(name = "Excel导入")
@Slf4j
public class ExcelImportController {

    @Autowired
    private PoiService poiService;

    @Operation(summary = "apache poi方式导入")
    @PostMapping("/excel01")
    public void importExcel01(@RequestParam(value = "file",required = true) MultipartFile file) {
        try {
            poiService.importExcel01(file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Operation(summary = "easy excel方式导入")
    @PostMapping("/easyExcel")
    public String importEasyExcel(@RequestParam(value = "file") MultipartFile file) {
        try {
            poiService.importEasyExcel(file.getInputStream());
        } catch (Exception e) {
            log.error("导入报错:{}",e.getMessage());
            return e.getMessage();
        }
        return "success";
    }
}
